/**
 * 
 */
package com.edocbase.desktop.plugins.control;

import java.util.UUID;

import com.edocbase.biz.diary.entities.DiaryEntity;
import com.edocbase.biz.diary.entities.catalog.Catalog;
import com.edocbase.biz.diary.entities.catalog.CatalogManager;
import com.edocbase.desktop.maildriver.DesktopApplication;
import com.edocbase.desktop.maildriver.MDiaryConstants;
import com.edocbase.desktop.mht.MHTCompiler;
import com.edocbase.desktop.plugins.mdairy.parts.DiaryIndexable;
import com.edocbase.desktop.search.ISearchEngine;
import com.edocbase.desktop.swt.widgets.RichBrowser;
import com.edocbase.entity.EntityManager;
import com.edocbase.entity.EntityManagerFactory;

/**
 * @author X61
 * 
 */
public class DailyEditSaveProvider implements IRicheditSaveProvider {
	private HTMLRichTabItem richItem = null;

	/**
	 * 
	 */
	public DailyEditSaveProvider(HTMLRichTabItem item) {
		this.richItem = item;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.edocbase.desktop.plugins.control.IRicheditSaveProvider#save()
	 */
	@Override
	public DiaryEntity save(boolean autoSend) {
		DiaryEntity diary = richItem.getDiary();
		RichBrowser richBrowser = richItem.htmlViewer.getRichBrowser();
		diary.setDiaryType(MDiaryConstants.DiaryType.Daily.name());
		try {
			//DiaryLog log = new DiaryLog();
			String plainText = richBrowser.getPlainText();
			String text = richBrowser.getText();

//			Html2MHTCompiler mhtCompiler = new Html2MHTCompiler(text, diary
//					.getOutputStream());
//			mhtCompiler.compile();
			if (diary.getId() == 0) {
				//log.setType(MDiaryConstants.LOG_NEW_TYPE);
				diary.setContentId(UUID.randomUUID().toString());
			}
			new MHTCompiler().compile(text, diary.getOutputStream());
			
//			String dayName = DateFormatUtils.format(new Date(), "yyyy-MM-dd");
//			Catalog dailyCatalog = CatalogManager.getManager()
//					.getDailyLogCatalog();
			Catalog catalog = CatalogManager.getManager().UNKNOW;// dailyCatalog.getChildByName(dayName);

			diary.setCatalogId(catalog.getId());
			diary.setCatalogPath(catalog.getPath());
			//log.setType(MDiaryConstants.LOG_EDIT_TYPE);

			// this.diary.increaseVersion();
			diary.setContentDirty(true);
			diary.save();
			//log.setDiaryId(diary.getId());
			String content = null;// diary.getContent();

			content = plainText;
			String title = diary.getTitle();
			//log.setTitle(title);
			title = title == null ? "" : title;
			if (title.length() < 1) {
				title = richBrowser.getTitle();
			}
			if (title.length() < 1) {
				int length = Math.min(100, content.length());
				title = content.substring(length);
			}
			ISearchEngine searcher =DesktopApplication.getPlatform().getSearchEngine();
//			searcher.addDocument(String.valueOf(diary.getId()), catalog
//					.getPath(), title, content);
			searcher.addIndexable(new DiaryIndexable(diary,plainText));
			//richItem.setId(diary.getDiaryId());

//			if (autoSend) {
//				Set<Integer> set = diary.getAddresses();
//				Iterator<Integer> iterator = set.iterator();
//				if (iterator.hasNext()) {
//					Integer webId = (Integer) iterator.next();
//					if (webId != null) {
//						IWebAccount account = richItem.accountManager.get(webId);
//						DriverWorker
//								.getWorker().send(account, diary);
////						log.setContent(account.getFullAccount());
////						if (log.getType() == MDiaryConstants.LOG_NEW_TYPE) {
////							log.setType(MDiaryConstants.LOG_NEW_SEND_TYPE);
////						} else {
////							log.setType(MDiaryConstants.LOG_EDIT_SEND_TYPE);
////						}
//					}
//				}
//			}
			//log.setDate(new Date().getTime());
			//EntityManagerFactory.getEntityManager(DiaryLog.class).save(log);
			EntityManager<DiaryEntity> dailyManager = EntityManagerFactory.getEntityManager(DiaryEntity.class);
			
//			if (!dailyManager.exists(diary)) {
//				DailyEntity daily = new DailyEntity(diary.getId());
//				EntityManagerFactory.getEntityManager(DailyEntity.class).insert(daily);
//			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return diary;
	}

}
